import { _decorator, Component, Node, director, Sprite, Label, ProgressBar, SpriteFrame, loader, Prefab } from 'cc';
import { EventManager } from '../event/EventManager';
// import { MyEventType, eventTarget } from '../event/MyEvent';
const { ccclass, property } = _decorator;

import ResManager from '../common/ResManager';
import { UIManager } from '../common/UIManager';
import { UserInfo } from '../common/UserInfo';
import { Utils } from '../common/Utils';
import { PrefabEnum } from '../common/PrefabEnum';
import { shop } from '../shop/Shop';


@ccclass('SwitchWorldMap')
export class SwitchWorldMap extends Component {
    @property(Node)
    resLayout: Node
    @property(Label)
    private foodNum: Label
    @property(Label)
    private woodNum: Label
    @property(Label)
    private mudNum: Label
    @property(Label)
    private ironNum: Label
    @property(Label)
    private consumeNum: Label
    @property(Label)
    private nameString: Label
    @property(Label)
    private username: Label

    @property(Label)
    private diamond: Label
    @property(Node)
    private changeVillageBtn: Node;
    @property(Label)
    private coord: Label
    @property(Node)
    recharge: Node
    @property(Label)
    tili: Label

    onLoad() {
    }

    async start() {

        await UserInfo.instance.refreshVillageDetail()
      

        EventManager.instance.on(EventManager.EventType.GetVillageDetail, this.updateDetail, this)
        EventManager.instance.on(EventManager.EventType.RefreshDiamond, this.onRefreshDiamond, this)

        this.changeVillageBtn.on(Node.EventType.TOUCH_END, this.onChangeVillage, this)
        this.recharge.on(Node.EventType.TOUCH_END, this.onRecharge, this)
        this.resLayout.on(Node.EventType.TOUCH_END, this.onResLayout, this)

    }
    protected onDisable(): void {
        EventManager.instance.off(EventManager.EventType.GetVillageDetail, this.updateDetail, this)
        EventManager.instance.off(EventManager.EventType.RefreshDiamond, this.onRefreshDiamond, this)
    }

    update(deltaTime:number){
        let villageData = UserInfo.instance.villageData
        if(!villageData){
            return
        }
        // 每秒粮食的增加量
        let addFood = (villageData.res.food_output - villageData.Consume) / 3600
        villageData.res.food_reserve += addFood * deltaTime
        if(villageData.res.food_reserve < 0){
            // UserInfo.instance.refreshVillageDetail()
            return
        }

        villageData.res.wood_reserve += villageData.res.wood_output / 3600 * deltaTime
        villageData.res.iron_reserve += villageData.res.iron_output / 3600 * deltaTime
        villageData.res.mud_reserve += villageData.res.mud_output / 3600 * deltaTime

        if(villageData.res.wood_reserve > villageData.res.comm_max_reserve){
            villageData.res.wood_reserve = villageData.res.comm_max_reserve
        }
        if(villageData.res.mud_reserve > villageData.res.comm_max_reserve){
            villageData.res.mud_reserve = villageData.res.comm_max_reserve
        }
        if(villageData.res.iron_reserve > villageData.res.comm_max_reserve){
            villageData.res.iron_reserve = villageData.res.comm_max_reserve
        }
        if(villageData.res.food_reserve > villageData.res.food_max_reserve){
            villageData.res.food_reserve = villageData.res.food_max_reserve
        }

        this.updateDetail()
    }

    async updateDetail() {
        let data = UserInfo.instance.villageData
        // this.text1.string = Utils.bigNumberTransform(data.res.comm_max_reserve)
        // this.text2.string = Utils.bigNumberTransform(data.res.food_max_reserve)
        this.consumeNum.string = Utils.bigNumberTransform(data.Consume)
        // this.proBar.progress = data.Consume / data.res.food_output


        this.ironNum.string = Utils.bigNumberTransform(data.res.iron_reserve) // 铁
        this.woodNum.string = Utils.bigNumberTransform(data.res.wood_reserve) // 木
        this.mudNum.string = Utils.bigNumberTransform(data.res.mud_reserve)  // 石
        this.foodNum.string = Utils.bigNumberTransform(data.res.food_reserve)  // 粮食

        // this.twoBar1.progress =data.res.iron_reserve / data.res.comm_max_reserve
        // this.twoBar2.progress = data.res.wood_reserve / data.res.comm_max_reserve
        // this.twoBar3.progress = data.res.mud_reserve / data.res.comm_max_reserve
        // this.twoBar4.progress = data.res.food_reserve / data.res.food_max_reserve

        this.nameString.string = data.villageName
        this.username.string = data.user.uInfo.name
        this.diamond.string = data.user.diamond
        this.coord.string = `(${data.xId},${data.yId})`
        this.tili.string = data.user.action.cur + ' / ' + data.user.action.max

    }

    onRefreshDiamond(num) {
        this.diamond.string = num + ''
    }



    onAi() {
        console.log('ai........')
    }

    onMenu() {
        console.log('onMenu........')
    }
    onBuild() {
        console.log('onBuild........')
    }
    onTrain() {
        console.log('onTrain........')
    }
    onWorldBtn() {
        if (director.getScene().name == "village") {
            UIManager.instance.loadScene('world')
        } else {
            UIManager.instance.loadScene('village')
        }
        console.log('onWorldBtn........')
    }
    onVill() {
        if (director.getScene().name == "village") {
            UIManager.instance.loadScene('villageResources')
        } else {
            UIManager.instance.loadScene('village')
        }
    }
    onWorldDynamics() {
        // director.loadScene("worldDynamics");
        console.log('onWorldDynamics..........')
    }
    test() {
        director.loadScene("scene");
    }

    onChangeVillage() {
        
        let villageObjList = UserInfo.instance.villageData.villageObjList
        let list = villageObjList.filter(v => {
            return v.zoneId != UserInfo.instance.villageData.curVid
        })
        if(list.length == 0){
            UIManager.instance.tip('您还没有分村')
            return
        }
        UIManager.instance.openUI(PrefabEnum.ChangeVillage)
    }

    async onRecharge() {
        let ui = await UIManager.instance.openUI(PrefabEnum.Shop)
        let com = ui.getComponent(shop)
        com.active = '获得元宝'
    }

    onResLayout() {
        UIManager.instance.openUI(PrefabEnum.VillageResources)
    }
}


